package wpmp.auth.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import wpmp.utils.quickbean.KeyValue;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 4:38:01 PM Mar 22, 2013
 */
@Component
public class KeyValueDao extends BaseDao {

	private static final String SQL_LIST_ALL = "SELECT u_key,value FROM sys_key_value";

	private static final String SQL_GET = "SELECT u_key,value FROM sys_key_value WHERE u_key=?";

	private static RowMapper<KeyValue> DEFAULT_ROW_MAPPER = new RowMapper<KeyValue>() {
		@Override
		public KeyValue mapRow(ResultSet rs, int rowNum) throws SQLException {
			KeyValue dest = new KeyValue();
			dest.setKey(rs.getString("u_key"));
			dest.setValue(rs.getString("value"));
			return dest;
		}
	};

	public KeyValue queryForObject(String key) {
		return getJdbcTemplate().queryForObject(SQL_GET, DEFAULT_ROW_MAPPER, key);
	}

	public List<KeyValue> queryForList() {
		return getJdbcTemplate().query(SQL_LIST_ALL, DEFAULT_ROW_MAPPER);
	}
}
